using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mani.Common;
using Ghasam.Business.Services;
using Ghasam.Business.Domain.Entity;
using System.Collections.Generic;

public partial class AccountingPage : BasePage
{
    public override string ActionName {
        get {
            return "vw_accounting";
        }
    }
    #region "Private Property"
    private IAccountingService accountingService;
    private ICustomerService customerService;
    private int? _customerId;
    private int _isFromal;
    private DateTime? _startDate = null;
    private DateTime? _endDate = null;
    
    #endregion

    public IAccountingService AccountingService
    {
        set { accountingService = value; }
        get {return accountingService; }
    }
    public ICustomerService CustomerService
    {
        set { customerService = value; }
        get { return customerService; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckCustomerType();
        fillCustomerList();
    }

    private void CheckCustomerType() {
        bool show = CommonHelper.ShowHiddenCustomers();
        gvAccounting.Columns[2].Visible = show;
        divHiddenCustomers.Visible = show;
    }

     protected void CustomerTypeCheckedChanged( object sender, EventArgs e ) {
        gvAccounting.DataBind();
    }

    private void fillCustomerList()
    {
        Customer customer = new Customer();
        checkFormalCustomer();
        customer.IsFormal =Convert.ToBoolean(_isFromal);
        ddlCustomers.DataSource = customerService.GetCustomers(customer,1000,0);
        ddlCustomers.DataBind();
    }
    private void checkFormalCustomer()
    {
        _isFromal = 0;
        bool show = CommonHelper.ShowHiddenCustomers();
        if (show && rbHiddenCustomers.Checked)
        {
            _isFromal = 1;
        }
        else if (show && rbAll.Checked)
        {
            _isFromal = -1;
        }
    }
    private void getControlsValue()
    {
        if (ddlCustomers.SelectedItem.Value != "-1") _customerId = Convert.ToInt32(ddlCustomers.SelectedItem.Value);
        checkFormalCustomer();
    
        if (String.IsNullOrEmpty(txtStartDate.Text))
        {
         _startDate = new DateTime(2003, 12, 12);
         }
        else
        {
         _startDate = MyPersianDate.PersianToDateTime(txtStartDate.Text);
        }

         if (String.IsNullOrEmpty(txtEndDate.Text))
        {
         _endDate = new DateTime(2060, 12, 12);
        }
         else
        {
         _endDate = MyPersianDate.PersianToDateTime(txtEndDate.Text);
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
       getControlsValue();
       //ViewState["gvAccounting"] = AccountingService.FindAccounts(_customerId, Convert.ToBoolean(_isFromal), _startDate, _endDate);
       gvAccounting.DataSource = AccountingService.FindAccounts(_customerId, Convert.ToBoolean(_isFromal), _startDate, _endDate);
       gvAccounting.DataBind();
       Session["gv"] = gvAccounting.DataSource as IList<Accounting>;
    }

    protected void gvAccounting_PageIndexChanging(object sender, GridViewPageEventArgs e) {
        gvAccounting.PageIndex = e.NewPageIndex;

        getControlsValue();
        gvAccounting.DataSource = AccountingService.FindAccounts(_customerId, Convert.ToBoolean(_isFromal), _startDate, _endDate);
        gvAccounting.DataBind();
    }
    static int numberDiv = 0;
    protected void gvAccounting_Sorting(object sender, GridViewSortEventArgs e)
    {
        getControlsValue();
        bool dir;
        if ((numberDiv % 2) == 0)
            dir = true;
        else
            dir = false;
        numberDiv++;
        gvAccounting.DataSource = AccountingService.FindAccountsBySorting(_customerId, Convert.ToBoolean(_isFromal), _startDate, _endDate, e.SortExpression, dir);
        gvAccounting.DataBind();
    }

}
